#include "mainmenu.h"
#include "buttonitem.h"
#include "dialogitem.h"
#include "version.h"
#include <QDebug>

MainMenu::MainMenu(QGraphicsItem *parent, QGraphicsScene *scene):
    QGraphicsItem(parent, scene)
{
    initScene();
    initEventSystem();
}

void MainMenu::initScene()
{

    background = new ImageItem(":/menu-background", QPoint(),
                               QPoint(ImageItem::BG_SIZE.width(),
                                      ImageItem::BG_SIZE.height()),
                               scene(), NULL);

    aboutDialog = new DialogItem(":/dialog-about", QPoint(200, 77),
                                 QPoint(600, 477), scene(), background);
    QGraphicsTextItem *author = new QGraphicsTextItem("Author: Speeder", aboutDialog, scene());
    author->setFont(QFont("Janis", 36));
    author->setPos(40, 120);
    QGraphicsTextItem *version = new QGraphicsTextItem("Version: "+Version::version(), aboutDialog, scene());
    version->setFont(QFont("Janis", 36));
    version->setPos(32, 200);


    exitBg = new ButtonItem(":/menu-exit", QPoint(654, 359),
                            QPoint(759, 431), scene(), background);
    exitBg->setZValue(2);
    qDebug() << exitBg->pos();

    levelBg = new ButtonItem(":/menu-level", QPoint(324, 369),
                            QPoint(448, 434), scene(), background);

    optionBg = new ButtonItem(":/menu-option", QPoint(491, 315),
                            QPoint(608, 389), scene(), background);

    aboutBg = new ButtonItem(":/menu-about", QPoint(29, 314),
                            QPoint(143, 397), scene(), background);

    editorBg = new ButtonItem(":/menu-editor", QPoint(166, 366),
                            QPoint(287, 443), scene(), background);

//    this->setAcceptHoverEvents(true);
//    this->setEnabled(true);
}

void MainMenu::initEventSystem()
{
    connect(exitBg, SIGNAL(clicked(ButtonItem*)), this, SLOT(clicked(ButtonItem*)));
    connect(editorBg, SIGNAL(clicked(ButtonItem*)), this, SLOT(clicked(ButtonItem*)));
    connect(aboutBg, SIGNAL(clicked(ButtonItem*)), this, SLOT(clicked(ButtonItem*)));
    connect(optionBg, SIGNAL(clicked(ButtonItem*)), this, SLOT(clicked(ButtonItem*)));
    connect(levelBg, SIGNAL(clicked(ButtonItem*)), this, SLOT(clicked(ButtonItem*)));
    connect(aboutDialog, SIGNAL(dialogClosed()), this, SLOT(dialogClosed()));
}

void MainMenu::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

}

QRectF MainMenu::boundingRect() const
{
    return scene()->sceneRect();
}

void MainMenu::clicked(ButtonItem *who)
{
    if (exitBg == who)
        emit exitClicked();
    else if (editorBg == who)
        emit editorClicked();
    else if (aboutBg == who)
        popupAboutDialog();
    else if (levelBg == who)
        emit levelClicked();
    else {}
}

void MainMenu::popupAboutDialog()
{
    enableButtons(false);
    aboutDialog->popup();
}

void MainMenu::dialogClosed()
{
    aboutBg->setFocusing(false);
    enableButtons(true);
}

void MainMenu::enableButtons(bool enabled)
{
    exitBg->setEnabled(enabled);
    levelBg->setEnabled(enabled);
    optionBg->setEnabled(enabled);
    aboutBg->setEnabled(enabled);
    editorBg->setEnabled(enabled);
}

